﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight;
using WorkSpace.UI.Windows;
using System.Json;
using WorkSpace.UI.Controls;
using WorkSpace.UI.WorkSpaceRef;
using System.Collections.ObjectModel;

namespace WorkSpace.UI.Controls
{
    public partial class PersonalCategoryPart : UserControl
    {
        private List<PackagePart> _packageParts;
        private List<Button> _btns;

        private Category _category;
        private string _account;
        private Guid _currentPackageId;

        public Category Category { get { return _category; } }
        public ObservableCollection<PersonalPackage> Packages { get; private set; }

        public Action<ObservableCollection<PersonalPackage>, PackagePart> PackagesChanged;

        public PersonalCategoryPart(Category category, string account)
        {
            InitializeComponent();

            _category = category;
            _account = account;

            this.SizeChanged+=new SizeChangedEventHandler(PersonalCategoryPart_SizeChanged);
            this._ctlPanel.SizeChanged += new SizeChangedEventHandler(_ctlPanel_SizeChanged);
            this.Loaded += new RoutedEventHandler(PersonalCategoryPart_Loaded);
        }

        #region 重新调整大小
        void PersonalCategoryPart_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (_packageParts != null)
            {
                foreach (var part in _packageParts)
                {
                    part.Width = _ctlScroller.ActualWidth;
                    part.Height = _ctlScroller.ActualHeight;
                }
            }
        }

        void _ctlPanel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (_packageParts != null)
            {
                for (int index = 0; index < Packages.Count; index++)
                {
                    PersonalPackage package = Packages[index];
                    if ((_currentPackageId == Guid.Empty && index == 0)
                        || (package.Id == _currentPackageId))
                    {
                        this._ctlScroller.ScrollToHorizontalOffset(_ctlScroller.ActualWidth * index);
                    }
                }
            }
        }
        #endregion

        void PersonalCategoryPart_Loaded(object sender, RoutedEventArgs e)
        {
            if (_packageParts == null || _packageParts.Count == 0)
            {
                LoadDatas();
            }
        }

        public void LoadDatas(Guid packageId)
        {
            _currentPackageId = packageId;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetPersonalPackagesCompleted += new EventHandler<WorkSpaceRef.GetPersonalPackagesCompletedEventArgs>(client_GetPersonalPackagesCompleted);
            client.GetPersonalPackagesAsync(_category.Id, _account);
        }

        public void LoadDatas()
        {
            _currentPackageId = Guid.Empty;

            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetPersonalPackagesCompleted += new EventHandler<WorkSpaceRef.GetPersonalPackagesCompletedEventArgs>(client_GetPersonalPackagesCompleted);
            client.GetPersonalPackagesAsync(_category.Id, _account);
        }

        void client_GetPersonalPackagesCompleted(object sender, GetPersonalPackagesCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                Packages = e.Result.Data;

                Bind();
            }
        }

        private void Bind()
        {
            _packageParts = new List<PackagePart>();
            _btns = new List<Button>();
            _ctlPanel.Children.Clear();
            _ctlBtnContainer.Children.Clear();

            PackagePart currentPakcage = null;

            for (int index = 0; index < Packages.Count; index++)
            {
                PersonalPackage package = Packages[index];
                RadioButton btn = new RadioButton();
                ToolTipService.SetToolTip(btn, package.Caption);
                btn.Width = 16;
                btn.Height = 16;
                btn.Margin = new Thickness(5, 0, 0, 0);
                btn.Tag = index;
                btn.Checked += new RoutedEventHandler(btn_Checked);
                _ctlBtnContainer.Children.Add(btn);

                PackagePart part = new PackagePart(package);
                part.Width = _ctlScroller.ActualWidth;
                part.Height = _ctlScroller.ActualHeight;
                _ctlPanel.Children.Add(part);
                _packageParts.Add(part);

                if ((_currentPackageId == Guid.Empty && index == 0)
                    || (package.Id == _currentPackageId))
                {
                    currentPakcage = part;
                    btn.IsChecked = true;
                }
            }

            if (PackagesChanged != null)
            {
                PackagesChanged(Packages, currentPakcage);
            }
        }

        void btn_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            int index = (int)btn.Tag;
            _currentPackageId = _packageParts[index].Package.Id;

            Storyboard storyboard = new Storyboard();

            DoubleAnimation animation = new DoubleAnimation();
            animation.To = _ctlScroller.ActualWidth * index;
            animation.Duration = TimeSpan.FromMilliseconds(500);
            animation.RepeatBehavior = new RepeatBehavior(1);
            animation.AutoReverse = false;
            Storyboard.SetTarget(animation, this._ctlHScrollingWrapper);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Value"));
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }

        private void _ctlBtnSettings_Click(object sender, RoutedEventArgs e)
        {
            PackagePart curPackage = null;

            for (int index = 0; index < Packages.Count; index++)
            {
                PackagePart packagePart = _packageParts[index];
                if ((_currentPackageId == Guid.Empty && index == 0)
                    || (packagePart.Package.Id == _currentPackageId))
                {
                    curPackage = packagePart;
                    break;
                }
            }

            PersonalCategoryWindow personalCategoryWindow = new PersonalCategoryWindow(this, curPackage, _account);
            this.PackagesChanged = personalCategoryWindow.PackagedSourceChanged;
            personalCategoryWindow.Closed += new EventHandler(personalCategoryWindow_Closed);
            personalCategoryWindow.Show();
        }

        void personalCategoryWindow_Closed(object sender, EventArgs e)
        {
            PersonalCategoryWindow personalCategoryWindow = sender as PersonalCategoryWindow;
            if (personalCategoryWindow.DialogResult == true)
            {
                this.PackagesChanged = null;
            }
        }
    }
}
